const { list, save, find, close } = require('./data/charge-records')
const base = '/operation'
module.exports = [{
  url: `${base}/chargeOrders/[1-9][0-9]*`,
  type: 'get',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = find(id)
    if (r) {
      return { success: true, data: r }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}, {
  url: `${base}/chargeOrders`,
  type: 'get',
  response: config => {
    const { page, limit, orgUnitId, roomId } = config.query
    return list(page, limit, orgUnitId, roomId)
  }
}, {
  url: `${base}/chargeOrders`,
  type: 'post',
  response: config => {
    const { roomId, amount, payType } = config.body
    const id = save(roomId, amount, payType)
    return { success: true, data: { id, payCode: JSON.stringify({ id, roomId, amount, payType }) }}
  }
}, {
  url: `${base}/chargeOrders/[1-9][0-9]*`,
  type: 'put',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    close(id)
    return { success: true }
  }
}]
